package com.higgins.eventtracking.client.health.fundamentals;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.components.WeekNumberListBox;

public class NegativeSubTab extends VerticalPanel implements ChangeHandler, GenericTabInterface		{
	private ScrollPanel achievementsScrollPanel = new ScrollPanel();
	private final EventTrackingServiceAsync greetingService = GWT
														.create(EventTrackingService.class);
	private WeekNumberListBox weekNumberListBox= new WeekNumberListBox();
	private VerticalPanel panel = new VerticalPanel();
	private boolean firstRun = true;
	private String frameOfMind = null;
	private NegativeTab parentTab = null;
	private String tabTitle = null;
	private Integer tabIndex = null;

	
	public NegativeSubTab(NegativeTab parentTab, String frameOfMind)	{
		this.parentTab = parentTab;
		this.frameOfMind = frameOfMind;
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		setHeight("20em");
		setWidth("800px");

		addWeekNumberListBox();
		populateAchievements();
		add(achievementsScrollPanel);
		firstRun = false;
	}
	public void addWeekNumberListBox()	{
		add(weekNumberListBox);
		weekNumberListBox.addChangeHandler(this);
		populateAchievements();
	}
	private void populateAchievements()	{
		greetingService.retrieveAchievements(frameOfMind, weekNumberListBox.getSelectedIndex(), new AsyncCallback<List<String>>()	{
			@Override
			public void onFailure(Throwable caught) {
				parentTab.setSubHeaderCount(frameOfMind, 0);
			}
			@Override
			public void onSuccess(List<String> results) {
				addAchievements(results);
				if (weekNumberListBox.getSelectedIndex()==0)	{
					parentTab.setSubHeaderCount(frameOfMind, results.size());
					parentTab.updateTabTitle(tabIndex, frameOfMind + "(" + results.size() + ")", false);
				}
			}
		});		
	}
	
	private void addAchievements(List<String> results)	{
		try	{
			achievementsScrollPanel.remove(panel);
		} catch (Exception e)	{
			
		}
		panel = new VerticalPanel();
		for (String result:results)	{
			Label label = new Label(result);
			panel.add(label);
			panel.setCellHeight(label, Integer.toString(label.getElement().getOffsetHeight())+"px");
		}
		panel.setHeight(String.valueOf(results.size())+ "em");
		achievementsScrollPanel.add(panel);
		achievementsScrollPanel.setHeight("15em");

	}
	@Override
	public void onChange(ChangeEvent event) {
		populateAchievements();
	}
	@Override
	public String getTabTitle() {
		return tabTitle;
	}

	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}

	@Override
	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}	
}
